/**
 * @file: app.c
 * @brief: APP层
 * @author: liaowx
 * @date: 2019.08.03
 */
#include <stdio.h>
#include <malloc.h>
#include <string.h>

#include "app.h"
#include "core.h"

static void app_open(struct app *app, struct core *core);

int main(int argc, char *argv[])
{
    struct app *app = (struct app *)malloc(sizeof(struct app));
    struct core *core;
    int cmd;

    app->core = (struct core *)malloc(sizeof(struct core));

    /*来自APP的信息*/
    app->file_name = argv[2];
    app->arg = argv[1];
    app->app_open = app_open;

    /* 填充app中core的信息 */
    app->app_open(app, app->core);

    /* 向中间层拿去底层信息 */
    core = mode_ctl(GET_DEV_CTL, NULL);
    /* 匹配APP层与底层 */
    cmd = match(app->arg, core);
    if (cmd == -1) {
        printf("没有匹配项\n");
        return -1;
    }

    core[cmd].file_name = app->file_name;

    core = &core[cmd];
    app->core->core_ctl(COPY, core);

    return 0;
}

static void app_open(struct app *app, struct core *core)
{
    core->file_name = app->file_name;
    core->arg[CMD] = app->arg;

    core->core_ctl = mode_ctl;
}